package com.example.makerspace.manager;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.makerspace.dao.TechnicDockingDAO;
import com.example.makerspace.domain.TechnicDockingDO;
import com.example.makerspace.dto.*;
import com.example.makerspace.enums.ResultCodeEnum;
import com.example.makerspace.utils.FileUtils;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.example.makerspace.service.impl.AdminServiceImpl.managerMap;

@Component
public class TechnicManager extends BaseManager<TechnicDockingDO, TechnicDTO>{
    private final static String TECHNIC_PICTURE_PATH_PREFIX = "technic/";
    private final static List<Map<String, String>> FUZZY_QUERY_PARAMS = Lists.newArrayList(
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "technic_name").put(QUERY_TYPE, QUERY_TYPE_MATCH_PHRASE).build(),
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "keyword").put(QUERY_TYPE, QUERY_TYPE_MATCH_PHRASE).build()
    );
  private  final  static  String   testHttpPrefix="https://119.45.199.13:3035/static/";
   private  final  static  String  devHttpPrefix="https://nism.ytcata.com:443/static/";

    @Resource
    private TechnicDockingDAO technicDockingDAO;

    @PostConstruct
    public void init(){
        this.baseDAO = technicDockingDAO;
        managerMap.put("technic", this);
    }
    public Result addTechnic(TechnicDTO technicDTO){
        TechnicDockingDO oldTechnicDockingDO = technicDockingDAO.selectByTechnicName(technicDTO.getTechnicName());
        if(oldTechnicDockingDO != null){
            return Result.error(ResultCodeEnum.TECHNIC_EXIST);
        }
        String imagePath = FileUtils.saveMultipartFile(technicDTO.getPicture(), FileUtils.staticPath + TECHNIC_PICTURE_PATH_PREFIX + technicDTO.getTechnicName());
        TechnicDockingDO technicDockingDO = new TechnicDockingDO();
        BeanUtils.copyProperties(technicDTO, technicDockingDO);
        technicDockingDO.setPicture(imagePath);
        technicDockingDAO.insert(technicDockingDO);
        return Result.success();
    }

    public Result updateTechnic(TechnicDTO technicDTO){
        TechnicDockingDO technicDockingDO = technicDockingDAO.selectById(technicDTO.getId());
        if(technicDockingDO == null){
            return Result.error(ResultCodeEnum.TECHNIC_NOT_EXIST);
        }
        Optional.ofNullable(technicDTO.getTechnicName()).ifPresent(technicDockingDO::setTechnicName);
        Optional.ofNullable(technicDTO.getKeyword()).ifPresent(technicDockingDO::setKeyword);
        Optional.ofNullable(technicDTO.getTechnicDesc()).ifPresent(technicDockingDO::setTechnicDesc);
        Optional.ofNullable(technicDTO.getMeeting()).ifPresent(technicDockingDO::setMeeting);
        Optional.ofNullable(technicDTO.getPicture()).ifPresent(picture ->{
            FileUtils.deleteFile(technicDockingDO.getPicture());
            String imagePath = FileUtils.saveMultipartFile(picture, FileUtils.staticPath + TECHNIC_PICTURE_PATH_PREFIX + technicDTO.getTechnicName());
            technicDockingDO.setPicture(imagePath);
        });
        return updateDO(technicDockingDO);
    }

    public TechnicDTO convert2DTO(TechnicDockingDO technicDockingDO){
        TechnicDTO technicDTO = super.convert2DTO(technicDockingDO);
        String picture = technicDockingDO.getPicture();
        String briefPath = FileUtils.getBriefPath(picture);
        String path = devHttpPrefix + briefPath;
        technicDTO.setPicturePath(path);
        return technicDTO;
    }

    @Override
    public PageResult list(PageRequest pageRequest){
        Map<String, Object> params = pageRequest.getParams();
        Pagination pagination = pageRequest.getPagination();
        String fuzzy = MapUtils.getString(params, "fuzzy");
        if(StringUtils.isBlank(fuzzy)){
            return super.list(pageRequest);
        }
        return search(null, null, FUZZY_QUERY_PARAMS, fuzzy, pagination, TechnicDockingDO.class);
    }
}
